<template>
  <div>
    <el-form size="mini" :inline="true" :model="queryMap">
      <el-form-item label="部门名称">
        <el-input v-model="queryMap.name" placeholder="请输入部门查询" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getDepartmentList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <el-table
      v-loading="dataListLoading"
      :data="departmentData"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      style="width: 100%;">
      <!--<el-table-column prop="id" type="index" label="ID" width="50"></el-table-column>-->
      <!--<el-table-column prop="phone" label="办公电话" width="180"></el-table-column>-->
      <el-table-column prop="name" label="部门名称" width="200"></el-table-column>
      <el-table-column prop="total" label="人数" width="100">
        <template slot-scope="scope">
          <el-tag effect="plain" v-text="scope.row.total+'人'" size="small">
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column prop="modifiedTime" label="修改时间"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.parentId !== 0"
            type="text"
            size="mini"
            icon="el-icon-edit"
            @click="addOrUpdateHandle(scope.row.id)"
          >编辑
          </el-button>

          <el-button
            v-if="scope.row.parentId !== 0"
            type="text"
            size="mini"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row.id)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update-dept v-if="addOrUpdateVisible" ref="addOrUpdateDept"
                        @refreshDataList="getDepartmentList"></add-or-update-dept>
  </div>
</template>

<script>
import AddOrUpdateDept from './department-add-or-update'

export default {
  data() {
    return {
      dataListLoading: false,
      // 存放部门数据
      departmentData: [],
      // 选择的数据
      dataListSelections: [],
      addOrUpdateVisible: false,
      // 搜索参数
      queryMap: {
        name: ''
      }
    }
  },
  components: {
    AddOrUpdateDept
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    // 获取部门数据列表
    getDepartmentList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sys/department/list'),
        method: 'get',
        params: this.$http.adornParams({
          'name': this.queryMap.name
        }),
      }).then(({data}) => {
        this.departmentData = data.departmentList
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        // 为子组件addOrUpdate传递部门数据
        this.$refs.addOrUpdateDept.dataFromDepartment = this.departmentData
        this.$refs.addOrUpdateDept.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      let Ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${Ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/department/delete'),
          method: 'post',
          data: this.$http.adornData(Ids, false)
        }).then(({data}) => {
          if (data && data.code === 200) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDepartmentList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {
      })
    },
  }
}
</script>

<style scoped>

</style>
